<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="预约订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入预约订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付渠道" prop="payChannel">
        <el-select v-model="queryParams.payChannel" placeholder="请选择支付渠道" clearable size="small">
          <el-option
            v-for="dict in payChannelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付类型" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付类型" clearable size="small">
          <el-option
            v-for="dict in payTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预约电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入预约电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="startTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.startTime"
                        type="datetime"
                        :picker-options="pickerOptions"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择下单开始时间">
        </el-date-picker>&ensp;-&ensp;
        <el-date-picker clearable size="small"
                        v-model="queryParams.endTime"
                        type="datetime"
                        :picker-options="pickerOptions"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择下单结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:serviceOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:serviceOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:serviceOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serviceOrderList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="订单编号" align="center" prop="orderNo" width="200"/>
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="分类名称" align="center" prop="courseName" />
      <el-table-column label="小区名称" align="center" prop="communityName" />
      <el-table-column label="预约人" align="center" prop="consignee" />
      <el-table-column label="预约电话" align="center" prop="phone" width="130"/>
      <el-table-column label="服务地址" align="center" prop="address" width="130"/>
      <el-table-column label="支付用户" align="center" prop="userName" />
      <el-table-column label="订单状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="支付渠道" align="center" prop="payChannel" :formatter="payChannelFormat" />
      <el-table-column label="应付金额" align="center" prop="actualPrice" >
        <template slot-scope="scope">
          <span style="color: red">{{scope.row.actualPrice / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="center" prop="payPrice" >
        <template slot-scope="scope">
          <span  style="color: red">{{scope.row.payPrice / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约服务时间" align="center" prop="gmtConfirm" width="135">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtConfirm, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createtime" width="135">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="paytime" width="135">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paytime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付流水号" align="center" prop="payNo" width="200"/>
      <el-table-column label="支付类型" align="center" prop="payType" :formatter="payTypeFormat" />
      <el-table-column label="服务人员姓名" align="center" prop="servicePeopleName" width="120"/>
      <el-table-column label="服务人员电话" align="center" prop="servicePeoplePhone" width="120"/>
      <el-table-column label="工号" align="center" prop="jobNum" />
      <el-table-column label="操作" align="center" fixed="right" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
             v-if="scope.row.status == 2"
            @click="sendPeople(scope.row)"
          >指派服务<i class="el-icon-caret-right"></i></el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished" v-if="scope.row.status == 6"
            @click="finishedOrder(scope.row)"
          >完成服务</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"   label-width="120px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" disabled placeholder="请输入预约订单号" />
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="form.status" disabled  placeholder="请选择订单状态" style="width: 100%">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" disabled  placeholder="产品名称" />
        </el-form-item>
        <el-form-item label="分类名称" prop="courseName">
          <el-input v-model="form.courseName"  disabled placeholder="分类名称" />
        </el-form-item>
        <el-form-item label="支付用户" prop="userName">
          <el-input v-model="form.userName" disabled  placeholder="支付用户" />
        </el-form-item>

        <el-form-item label="服务地址" prop="payNo">
          <el-input v-model="form.payNo" disabled  placeholder="服务地址" />
        </el-form-item>
        <el-form-item label="应付金额" prop="actualPrice">
          <el-input v-model="form.actualPrice" disabled  placeholder="请输入应付金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="payPrice">
          <el-input v-model="form.payPrice"  disabled placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="支付时间" prop="paytime">
          <el-date-picker clearable size="small"
            v-model="form.paytime" disabled
                          type="datetime"  style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付渠道" prop="payChannel">
          <el-select v-model="form.payChannel" placeholder="请选择支付渠道" disabled   style="width: 100%">
            <el-option
              v-for="dict in payChannelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付流水号" prop="payNo">
          <el-input v-model="form.payNo" disabled  placeholder="请输入支付流水号" />
        </el-form-item>


        <el-form-item label="支付类型" prop="payType">
          <el-select v-model="form.payType"  disabled placeholder="请选择支付类型"  style="width: 100%">
            <el-option
              v-for="dict in payTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预约地址" prop="address">
          <el-input v-model="form.address" disabled  placeholder="请输入预约地址" />
        </el-form-item>
        <el-form-item label="预约电话" prop="phone">
          <el-input v-model="form.phone" disabled  placeholder="请输入预约电话" />
        </el-form-item>
        <el-form-item label="预约人" prop="consignee">
          <el-input v-model="form.consignee"  disabled placeholder="请输入预约人" />
        </el-form-item>
        <el-form-item label="预约服务时间" prop="gmtConfirm">
          <el-date-picker clearable size="small"
                          v-model="form.gmtConfirm"  style="width: 100%"
                          type="datetime" disabled
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择预约服务时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务人员姓名" prop="servicePeopleName">
          <el-input v-model="form.servicePeopleName" disabled  placeholder="服务人员姓名" />
        </el-form-item>
        <el-form-item label="服务人员电话" prop="servicePeoplePhone">
          <el-input v-model="form.servicePeoplePhone" disabled  placeholder="服务人员电话" />
        </el-form-item>
        <el-form-item label="服务人员工号" prop="jobNum">
          <el-input v-model="form.jobNum" disabled  placeholder="服务人员工号" />
        </el-form-item>
        <el-form-item label="订单备注" prop="remark">
          <el-input v-model="form.remark" disabled  placeholder="订单备注" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 指派服务人员 -->
    <el-dialog :title="title" :visible.sync="openService" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"   label-width="120px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" disabled placeholder="请输入预约订单号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" disabled  placeholder="产品名称" />
        </el-form-item>
        <el-form-item label="预约人" prop="consignee">
          <el-input v-model="form.consignee"  disabled placeholder="请输入预约人" />
        </el-form-item>
        <el-form-item label="预约电话" prop="phone">
          <el-input v-model="form.phone" disabled  placeholder="请输入预约电话" />
        </el-form-item>
        <el-form-item label="预约地址" prop="address">
          <el-input v-model="form.address" disabled  placeholder="请输入预约地址" />
        </el-form-item>
        <el-form-item label="指派工作人员" prop="peopleId">
          <el-select v-model="form.peopleId" style="width: 100%"  placeholder="请选择指派工作人员" filterable clearable size="small" >
            <el-option
              v-for="people in servicePeopleList"
              :key="people.id"
              :label="people.name"
              :value="Number(people.id)"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listServiceOrder, getServiceOrder, delServiceOrder, addServiceOrder, updateServiceOrder, exportServiceOrder,serviceOK } from "@/api/system/serviceOrder";
import {listByTemp} from "@/api/system/servicePeople";
import Editor from "../../../components/Editor";
export default {
  name: "ServiceOrder",
  components: {
    Editor
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务订单管理表格数据
      serviceOrderList: [],
      //空闲服务人员列表
      servicePeopleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      pickerOptions: {
        disabledDate(time){
          return time.getTime() > Date.now()
        }
      },
      //指派服务人员
      openService: false,
      // 订单状态字典
      statusOptions: [],
      // 支付渠道字典
      payChannelOptions: [],
      // 支付类型字典
      payTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: undefined,
        status: undefined,
        payNo: undefined,
        payChannel: undefined,
        payType: undefined,
        phone: undefined,
        startTime: undefined,
        endTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        peopleId:[
          {required:true,message:"您尚未安排服务工作人员",trigger:"blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getPeopleList();
    this.getDicts("order_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("order_pay_channel").then(response => {
      this.payChannelOptions = response.data;
    });
    this.getDicts("order_pay_type").then(response => {
      this.payTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询服务订单管理列表 */
    getList() {
      this.loading = true;
      listServiceOrder(this.queryParams).then(response => {
        this.serviceOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getPeopleList(){
      listByTemp().then(response => {
        this.servicePeopleList = response.data;
      });
    },
    // 订单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 支付渠道字典翻译
    payChannelFormat(row, column) {
      return this.selectDictLabel(this.payChannelOptions, row.payChannel);
    },
    // 支付类型字典翻译
    payTypeFormat(row, column) {
      return this.selectDictLabel(this.payTypeOptions, row.payType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openService = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        orderNo: undefined,
        userId: undefined,
        status: undefined,
        price: undefined,
        actualPrice: undefined,
        payPrice: undefined,
        paytime: undefined,
        payNo: undefined,
        remark: undefined,
        payChannel: undefined,
        payType: undefined,
        communityId: undefined,
        aboutType: undefined,
        communityName: undefined,
        address: undefined,
        phone: undefined,
        consignee: undefined,
        peopleId: undefined,
        orderTag: undefined,
        createtime: undefined,
        updatetime: undefined,
        gmtConfirm: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.compareTime(this.queryParams.startTime,this.queryParams.endTime) == false){
        this.msgError("请选择正确的时间段");
        return false;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getServiceOrder(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改服务订单管理";
      });
    },
    //指派服务
    sendPeople(row){
      this.reset();
      const id = row.id;
      if (this.servicePeopleList.length < 1){
        this.msgError("没有空闲的服务工作人员啦！")
        return false;
      }
      getServiceOrder(id).then(response => {
        this.form = response.data;
        this.openService = true;
        this.title = "指派服务人员";
      });
    },
    //完成服务
    finishedOrder(row){
      const id = row.id ;
      this.$confirm('是否确认已完成订单号为"' + row.orderNo + '"的服务?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return serviceOK(id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.getPeopleList();
        this.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateServiceOrder(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("已安排工作人员上门服务，请致电该工作人员！ ");
              this.openService = false;
              this.getList();
              this.getPeopleList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除服务订单管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delServiceOrder(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有服务订单管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportServiceOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
